import { saveShopCart } from "../../../api/shopCartController";
import { getProductJoinList } from "../../../api/productController";
const AUTH = require('../../../utils/auth')
Component({
  options: {
    addGlobalClass: true,
  },
  properties: {
    wxlogin: {     // 注册成功 首页用来拦截
      type: Boolean,
      value: true
    }
  },
  data: {
    wxlogin: true,
    productList: [],
  },
  attached(){
    /**
     * 如果有custId 注册关于过 wxlogin 为true
     */
    this.getProductJoinList()
  },
  methods: {
    /**
     * 滾動
     */
    bindscrolltolower(e){
      console.log(e);
    },
    /**
     * 加盟产品列表
     */
    async getProductJoinList(){
      let custVipType = wx.getStorageSync('userBase').vipType || 0
      let resolve = await getProductJoinList({
        custVipType: custVipType,
      })
      if (resolve.success) {
        this.setData({
          productList: resolve.data
        })
      }
    },
    /**
     * 加盟
     */
    joinIn(e) {
      let self = this
      AUTH.checkHasLogined().then(isCustId => {
        if (!isCustId) {
          self.setData({
            custId: '',
            wxlogin: false,
          })
        }else{
        let {
            id
          } = e.currentTarget.dataset
          self.saveShopCart(id)
        }
      })
    },
    /**
     * 关闭弹窗
     */
    afterAuth(e) {
      console.log(e);
      this.setData({
        wxlogin: true,
      })
    },
    /**
     * 添加到购物车
     */
    async saveShopCart(id) {
      let self = this
      let resolve = await saveShopCart({
        productId: id,
        custId: wx.getStorageSync('custId'),
        count: 1
      })
      if (resolve.success) {
        wx.showToast({
          title: '添加成功！',
          icon: 'success',
          mask: true,
          success: ()=>{
            setTimeout(() => {
              self.triggerEvent('clickJoinIn', {
                PageCur: 'shoppingCartPage',
                id: id
              })
            }, 1500);
          }
        })
      }
    },
  }
})